package com.changgou.search.service.impl;

import com.alibaba.fastjson.JSON;
import com.changgou.goods.feign.SkuFeign;
import com.changgou.goods.pojo.Sku;
import com.changgou.search.dao.SkuEsMapper;
import com.changgou.search.pojo.SkuInfo;
import com.changgou.search.service.SkuService;
import entity.Result;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/***
 * 描述
 * @author ljh
 * @packagename com.changgou.search.service.impl
 * @version 1.0
 * @date 2020/5/10
 */
@Service
public class SkuServiceImpl implements SkuService {


    @Autowired
    private SkuFeign skuFeign;

    @Autowired
    private SkuEsMapper skuEsMapper;

    @Autowired
    private ElasticsearchTemplate elasticsearchTemplate;


    @Override
    public void importSku() {
        //1.调用商品微服务的feign 查询出符合条件的sku的数据
        //1.1 定义一个feign
        //1.2 添加feign的依赖
        //1.3 声明启用feign
        //1.4 调用
        Result<List<Sku>> byStatus = skuFeign.findByStatus("1");//获取正常的商品的列表
        List<Sku> data = byStatus.getData();//sku列表

        //2.调用spring data elasticsearch 的API 将数据导入到ES中
        //2.1 创建repository(dao)
        //2.2.调用方法保存数据到es中
        //将List<Sku> 转换给 List<SkuInfo>
        List<SkuInfo> skuInfoList = JSON.parseArray(JSON.toJSONString(data), SkuInfo.class);

        for (SkuInfo skuInfo : skuInfoList) {
            String spec = skuInfo.getSpec();//{"电视音响效果":"立体声","电视屏幕尺寸":"20英寸","尺码":"170"}
            Map<String, Object> specMap = JSON.parseObject(spec, Map.class);
            skuInfo.setSpecMap(specMap);//有值了
        }


        skuEsMapper.saveAll(skuInfoList);
    }

    //执行实现关键字搜索的功能
    @Override
    public Map search(Map<String, String> searchMap) {

        //0 从页面中获取要搜索的内容关键字
        String keywords = searchMap.get("keywords");

        if (StringUtils.isEmpty(keywords)) {//如果为空
            keywords = "华为";
        }

        //1.创建一个查询对象的 构建对象
        NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder();
        //2.设置查询的条件  匹配查询 从哪个索引 哪一个类型中查询 不指定就查询所有的索引和所有的类型
        nativeSearchQueryBuilder.withQuery(QueryBuilders.matchQuery("name", keywords));//匹配查询 特点：先分词再查询

        //3.构建查询对象
        SearchQuery query = nativeSearchQueryBuilder.build();
        //4.执行查询动作

        AggregatedPage<SkuInfo> skuInfos = elasticsearchTemplate.queryForPage(query, SkuInfo.class);
        //5.获取结果
        List<SkuInfo> content = skuInfos.getContent();//获取当前页的所有的记录
        long totalElements = skuInfos.getTotalElements();//获取总记录数
        int totalPages = skuInfos.getTotalPages();//获取总页数
        //6.设置结果封装对象返回
        Map<String, Object> resultMap = new HashMap<String, Object>();

        resultMap.put("rows", content);//当前页的结果集
        resultMap.put("total", totalElements);//总记录数
        resultMap.put("totalPages", totalPages);//总页数
        return resultMap;
    }
}















































































































































































































